package com.wwwarehouse.contract.contract_address;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.eventbus_event.BackListenerEvent;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.AddressListBean;
import com.wwwarehouse.contract.core.BaseViewPagerFragment;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.ChooseAddressRefreshEvent;
import com.wwwarehouse.contract.event.RefreshShoppCardEvent;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by feng.gao on 2017/10/19.
 * 管理地址 viewpager
 */

public class ManagerAddressViewPagerFragment extends BaseViewPagerFragment {
	private boolean isShow;
	private String demanId;
	private String mAddressUkid;

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		EventBus.getDefault().register(this);
		mConfirmBtn.setVisibility(View.GONE);
		mCustomScroll.setOnPullListener(new ElasticScrollView.OnPullListener() {
			@Override
			public void onDownPull() {
				if (!isShow) {
					((BaseCardDeskActivity) mActivity).downPull_3();
					((BaseCardDeskActivity)mActivity).setTitleBt1Text("新建地址");
					((BaseCardDeskActivity) mActivity).showTitleBt1();
					((BaseCardDeskActivity) mActivity).setTitleBt1Enable(true);

				} else {
					((BaseCardDeskActivity) mActivity).hideTitleBt();
				}
				isShow = !isShow;

			}

			@Override
			public void onUpPull() {

			}
		});

	}

	@Override
	public void titleBt1Click() {
		if (peekFragment() instanceof ManagerAddressViewPagerFragment) {
			//新建地址
			Bundle bundle = new Bundle();
			bundle.putString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID, demanId);
			bundle.putString(ContractConstant.KEY_ADDRESS_ADDRESSUKID, mAddressUkid);
			bundle.putString(ContractConstant.KEY_CONTRACT_ADD_ADDRESS, "add");
			UpdateAddressFragment addressFragment = new UpdateAddressFragment();
			addressFragment.setArguments(bundle);
			pushFragment(addressFragment);
		}
	}
	@Override
	public void onResponse(String response, int what) {
		if (what == 1) {
			AddressListBean addressListBean = JSON.parseObject(response, AddressListBean.class);
			addressListBean.setDemanId(demanId);
			if (addressListBean.getList() != null && addressListBean.getList().size() > 0) {
				setData(addressListBean, addressListBean.getList().size(), 7, ManagerAddressListViewFragment.class.getName());
			} else {
				mStateLayout.showEmptyView(true);
				mStateLayout.setEmptyListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						requestDatas();
					}
				});
			}
		}
	}

	@Override
	public void setTitle() {
		if (peekFragment() instanceof ManagerAddressViewPagerFragment) {
			mActivity.setTitle("管理地址");
		}
	}

	@Override
	public void requestDatas() {
		if (getArguments() != null && getArguments().getString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID) != null) {
			 demanId = getArguments().getString(ContractConstant.KEY_CONSTRACT_PLACE_ORDER_DEMANID);
			if (StringUtils.isNoneNullString(demanId)) {
				Map<String, Object> map = new HashMap<>();
				map.put("demandId", demanId);
				sendRequest(ContractConstant.ADDRESS_SELECT_ALL_ADDRESS_METHOD ,map , 1 );
			}

		}
		if (getArguments() != null && getArguments().getString(ContractConstant.KEY_ADDRESS_ADDRESSUKID) != null) {
			mAddressUkid = getArguments().getString(ContractConstant.KEY_ADDRESS_ADDRESSUKID);
		}
//		List<AddressListBean.ContractAddressBean> list = new ArrayList<>();
//		for (int i = 0; i < 30; i++) {
//			AddressListBean bean = new AddressBean();
//			list.add(bean);
//		}
//		AddressListBean listBean = new AddressListBean();
//		listBean.setList(list);
//		setData(listBean , list.size() ,  7 , new ManagerAddressListViewFragment().getClass().getName());
	}

	public void onEventMainThread(RefreshShoppCardEvent event) {
		if (peekFragment() instanceof ManagerAddressViewPagerFragment) {
			if (StringUtils.isNoneNullString(event.getRefresh())) {
				requestDatas();
			}
		}
	}


	@Override
	public void onDestroy() {
	        super.onDestroy();
	        EventBus.getDefault().unregister(this);
	    }

	public void onEventMainThread(BackListenerEvent backListenerEvent) {
		if (backListenerEvent.getMsg().equals("ManagerAddressViewPagerFragment")) {
			popFragment();
			//刷新选择地址界面
			EventBus.getDefault().post(new ChooseAddressRefreshEvent("refresh"));
		}
	}
}
